package com.base.sms.handler.qcloud;


import com.base.core.lang.StringUtils;
import com.base.sms.config.QCloudConfig;
import com.base.sms.model.enums.SMSHandlerEnum;
import com.base.sms.model.query.NoticeQuery;
import com.base.sms.handler.SendHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * 腾讯云发送处理
 *
 * @author guer
 */

public class QCloudSendHandler implements SendHandler {

    private static final String DEFAULT_NATION_CODE = "86";
    private static final Logger log = LoggerFactory.getLogger(QCloudSendHandler.class);

    private final QCloudConfig properties;

   // private final SmsMultiSender sender;

    public QCloudSendHandler(QCloudConfig properties) {
        this.properties = properties;
        //sender = new SmsMultiSender(properties.getAppId(), properties.getAppkey());
    }

    @Override
    public boolean send(NoticeQuery noticeQuery, Collection<String> phones) {
        String type = noticeQuery.getType();

        Integer templateId = properties.getTemplates(type);

        if (templateId == null) {
            log.debug("templateId invalid");
            return false;
        }

        List<String> paramsOrder = properties.getParamsOrder(type);

        ArrayList<String> params = new ArrayList<>();

        if (!paramsOrder.isEmpty()) {
            Map<String, String> paramMap = noticeQuery.getParams();
            for (String paramName : paramsOrder) {
                String paramValue = paramMap.get(paramName);

                params.add(paramValue);
            }
        }

        Map<String, ArrayList<String>> phoneMap = new HashMap<>(phones.size());

        for (String phone : phones) {
            if (StringUtils.isBlank(phone)) {
                continue;
            }
            if (phone.startsWith("+")) {
                String[] values = phone.split(" ");

                if (values.length == 1) {
                    getList(phoneMap, DEFAULT_NATION_CODE).add(phone);
                } else {
                    String nationCode = values[0];
                    String phoneNumber = StringUtils.join(values, "", 1, values.length);

                    getList(phoneMap, nationCode).add(phoneNumber);
                }

            } else {
                getList(phoneMap, DEFAULT_NATION_CODE).add(phone);
            }
        }

        return phoneMap.entrySet().parallelStream()
                .allMatch(entry -> sendByList(templateId, params, entry.getKey(), entry.getValue()));
    }

    @Override
    public String support() {
        return SMSHandlerEnum.QCLOUD.type();
    }

    private Collection<String> getList(Map<String, ArrayList<String>> phoneMap, String nationCode) {
        return phoneMap.computeIfAbsent(nationCode, k -> new ArrayList<>());
    }

    private boolean sendByList(int templateId, ArrayList<String> params, String nationCode, ArrayList<String> phones) {
        try {
//            SmsMultiSenderResult result = sender
//                    .sendWithParam(nationCode, phones, templateId, params, properties.getSmsSign(), "", "");
//
//            if (result.result == 0) {
//                return true;
//            }
//
//            log.debug("send fail[code={}, errMsg={}]", result.result, result.errMsg);
        } catch (Exception e) {
            log.debug(e.getMessage(), e);
        }

        return false;
    }
}
